/*监听路由变化获取参数*/
import {ref,watch,onActivated} from 'vue';
import {useRoute,onBeforeRouteLeave} from 'vue-router'

export const useRouteQuery = (callback) => {

  const route = useRoute();
  let routeQuery = ref(null);
  //监听路由，对比路由参数变化，没有变化不会执行
  watch(routeQuery,(val) =>{
    //路由参数变化触发方法
    console.log('路由参数变化',route.query);
    //页面条件赋值
    callback && callback(route.query)
    // query.value = route.query;
    // getDataList();
  })
  onBeforeRouteLeave(to => {
    //离开当前路由停止监听路由
    stopWatchRoute();
  });
  let stopWatchRoute = null;
  onActivated(() => {
    //进入该页面监听路由变化
    stopWatchRoute = watch(() => route.fullPath,
      (newValue, oldValue) => {
        routeQuery.value = newValue //赋值路由
      },
      { immediate: true }
    )
  });
}
